﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace 任务状态
{
  
    public class StockDataSaver
    {
        private string saveDirectory;
        private string stock;
        public StockDataSaver(string stock_)
        {
            ResetDir(stock_);
        }
        public void  ResetDir(string stock)
        {
            saveDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "StockData", stock);
            if (!Directory.Exists(saveDirectory))
            {
                Directory.CreateDirectory(saveDirectory);
            }
            this.stock = stock;
        }
        public void SaveData(string stock_, string data)
        {
            if(this.stock!= stock_)
            {
                this.ResetDir(stock_);
            }
            string sanitizedData = data.Replace("\r", "").Replace("\n", "");
            string date = DateTime.Now.ToString("yyyy-MM-dd");
            string filePath = Path.Combine(saveDirectory, $"{date}.txt");
            using (StreamWriter writer = new StreamWriter(filePath, append: true))
            {
                writer.WriteLine($"{DateTime.Now:HH:mm:ss} - {sanitizedData}");
                Console.WriteLine("保存数据到:" + filePath);
            }
        }
        public String[] LoadData(DateTime dateTime)
        {
            String path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "StockData", stock);
            string date = dateTime.ToString("yyyy-MM-dd");
            string filePath = Path.Combine(path, $"{date}.txt");
            String[] texts = File.ReadAllLines(filePath);
            return texts;
        }
    }

}
